<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 函数防抖
        // 在事件被触发n秒后再执行回调，如果在这n秒内又被触发，则重新计时
        function debounce(fn, delay) {
            var timeout
            return function() {
                var context = this
                var args = arguments
                clearTimeout(timeout)
                timeout = setTimeout(function() {
                    fn.apply(context, args)
                }, delay)
            }
        }
        // 使用
        window.onresize = debounce(function() {
            console.log('resize')
        }, 500)
    </script>
</body>
</html>